package top.mccsdl.Acbot.eventListener.listeners;

import cn.hutool.core.date.DateTime;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.mccsdl.Acbot.config.BotConfig;
import top.mccsdl.Acbot.config.EventName;
import top.mccsdl.Acbot.entity.KeyValue;
import top.mccsdl.Acbot.entity.Message;
import top.mccsdl.Acbot.entity.user.UserInfo;
import top.mccsdl.Acbot.eventListener.EventListener;
import top.mccsdl.Acbot.service.MenuService;
import top.mccsdl.Acbot.service.UserService;

import java.util.List;

import static top.mccsdl.Acbot.config.BotConfig.*;

/**
 * 频道私聊事件
 */
@Service
@EventName("DIRECT_MESSAGE_CREATE")
@Slf4j
public class DirectMessageCreateListener implements EventListener {

    @Autowired
    private BotConfig botConfig;
    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    @Override
    public void onEvent(JSONObject eventData) {
        String msgId = eventData.getString("id");//消息ID
        String guildId = eventData.getString("guild_id");
//        String srcGuildId = eventData.getString("src_guild_id");
        //发送的消息 content -> 测试
        String content = eventData.getString("content");
        JSONObject userInfo = eventData.getJSONObject("author");
        String userName = userInfo.getString("username");//用户名
        String userId = userInfo.getString("id");//用户名
        String avatar = userInfo.getString("avatar");//头像
        String union_openid = userInfo.getString("union_openid");//用户唯一ID
        String[] msg = content.trim().split("\\s+");
        //用户频道ID   ->author ->  id -> 9996957271968201140
        Message message = new Message();
        message.setMsgId(msgId);//被动消息
        UserInfo user = userService.channelPriGetUserInfo(userId, union_openid, guildId, message);
        log.info(DateTime.now() + "频道私聊:[{}]({})->{}", userName, union_openid, content);//用户艾特调用日志
        switch (msg[0]) {
            case "菜单", "/菜单" ->
                menuService.getMenuMsg(msg, message);
            case "绑定", "/绑定" -> {
            }
            case "查看", "/查看" -> {
            }
            case "初始化", "/初始化" -> {
            }
            default ->
                message.setContent("哎呀,未知命令捏");
        }

        String result = HttpRequest.post(String.format(DOMAIN + POST_CHANNELS_PRI_MSG, guildId))
                .body(message.toJsonStr())
                .header("Authorization", "QQBot " + botConfig.getAccessToken())
                .execute().body();
        log.info("频道私信消息事件发送返回===>" + result);
    }

}
